package com.dy.weixin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class TestMysql extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			process(req, resp);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			process(req, resp);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void process(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
		PrintWriter out = resp.getWriter();
		Connection conn = null;
		try {
		MysqlDataSource ds = new MysqlDataSource();
//		ds.setServerName("sqld.duapp.com");
		ds.setURL("jdbc:mysql://10.4.12.46:3306/d620554bc11934fd0a395dbc9f05c191e");
		ds.setUser("uHwwdxiwml9Ps");
		
		ds.setUseUnicode(true);
		ds.setPassword("phNCR8EZ6nL9n");
		ds.setCharacterEncoding("utf8");
		conn = ds.getConnection();
		out.println(conn.getMetaData().getURL());
		conn.close();
		} catch (Exception e){
			e.printStackTrace();
			out.println("error");
			out.println(e.getMessage());
		} finally {
			if (null != conn)
				conn.close();
		}
	}

	
}
